package api

import (
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
	"gorm.io/gorm"
	"math/rand"
	"path/filepath"
	"seat-service/initialization"
	"seat-service/model"
	"seat-service/model/dto"
	"seat-service/response"
	service "seat-service/service/impl"
	"strconv"
)

//uploads
//base64

var carouselService service.CarouselService

type CarouselApi struct {
}

func (*CarouselApi) AddCarousel(c *gin.Context) {
	var carDto dto.AddCarouselDto
	err := c.ShouldBindJSON(&carDto)
	if err != nil {
		response.Fail(c, response.DataInWrongFormat, nil)
		return
	}
	p, _ := strconv.Atoi(carDto.Priority)
	status, _ := strconv.Atoi(carDto.CarouselStatus)
	err = carouselService.AddCarousel(model.Carousel{
		Title:          carDto.Title,
		Priority:       p,
		Picture:        carDto.Picture,
		CarouselStatus: status,
	})
	if err != nil {
		response.Fail(c, response.DatabaseException, nil)
		return
	}
	response.Success(c, response.CodeSuccess, nil)
}
func (*CarouselApi) DelCarousel(c *gin.Context) {
	id := c.Query("id")
	numId, _ := strconv.Atoi(id)
	err := carouselService.DelCarousel(uint(numId))
	if err != nil {
		response.Fail(c, response.DatabaseException, nil)
		return
	}
	response.Success(c, response.CodeSuccess, nil)
}
func (*CarouselApi) UpdateCarousel(c *gin.Context) {
	var updateDto dto.UpdateCarouselDto
	err := c.ShouldBindJSON(&updateDto)
	if err != nil {
		response.Fail(c, response.DataInWrongFormat, nil)
		return
	}
	pri, _ := strconv.Atoi(updateDto.Priority)
	car, _ := strconv.Atoi(updateDto.CarouselStatus)
	id, _ := strconv.Atoi(updateDto.Id)
	err = carouselService.UpdateCarousel(model.Carousel{
		Model:          gorm.Model{ID: uint(id)},
		Title:          updateDto.Title,
		Priority:       pri,
		Picture:        updateDto.Picture,
		CarouselStatus: car,
	})
	if err != nil {
		response.Fail(c, response.DatabaseException, nil)
		return
	}
	response.Success(c, response.CodeSuccess, nil)
}
func (*CarouselApi) QueryCarousel(c *gin.Context) {
	page := c.Query("page")
	pageSize := c.Query("page_size")

	p, _ := strconv.Atoi(page)
	ps, _ := strconv.Atoi(pageSize)
	carousel, err := carouselService.QueryCarousel(p, ps)
	if err != nil {
		response.Fail(c, response.DatabaseException, nil)
		return
	}

	response.Success(c, response.CodeSuccess, carousel)
}
func (*CarouselApi) QueryCarouselByTitle(c *gin.Context) {
	title := c.Query("title")
	page := c.Query("page")
	pageSize := c.Query("page_size")

	p, _ := strconv.Atoi(page)
	ps, _ := strconv.Atoi(pageSize)
	carousel, err := carouselService.QueryCarouselByTitle(p, ps, title)

	if err != nil {
		response.Fail(c, response.DatabaseException, nil)
		return
	}

	response.Success(c, response.CodeSuccess, carousel)
}
func (*CarouselApi) PublishStatusCarousel(c *gin.Context) {
	var carDto dto.CarouselPubRetDto
	err := c.ShouldBindJSON(&carDto)
	if err != nil {
		response.Fail(c, response.DataInWrongFormat, nil)
		return
	}
	id, _ := strconv.Atoi(carDto.Id)

	err = carouselService.PublishStatusCarousel(uint(id))
	if err != nil {
		response.Fail(c, response.DatabaseException, nil)
		return
	}
	response.Success(c, response.CodeSuccess, nil)
}

func (*CarouselApi) UploadImage(c *gin.Context) {
	file, err := c.FormFile("file")
	if err != nil {
		response.Fail(c, response.DataInWrongFormat, nil)
		return
	}
	uploadDir := "./uploads/carousel"
	name := file.Filename
	file.Filename = strconv.Itoa(rand.Intn(1000)+1) + name
	// 生成保存文件的完整路径
	filePath := filepath.Join(uploadDir, file.Filename)

	// 保存文件
	err = c.SaveUploadedFile(file, filePath)
	if err != nil {
		response.Fail(c, response.ServerException, nil)
		return
	}
	response.Success(c, response.CodeSuccess, filePath)

}

func (*CarouselApi) Get(c *gin.Context) {
	data, err := carouselService.GetByStatus(1)
	if err != nil {
		initialization.SeatLogger.Error("carouselService.GetByStatus(1) failed", zap.Error(err))
		response.Fail(c, response.CodeServerBusy, nil)
		return
	}
	response.Success(c, response.CodeSuccess, data)

}

//uploads\\carousel\\1.png
